package com.ktgj.hotel.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.ktgj.common.core.enums.HotelEnum;
import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.hotel.domain.*;
import com.ktgj.hotel.domain.vo.BrandVo;
import com.ktgj.hotel.domain.vo.CityBrandVo;
import com.ktgj.hotel.mapper.*;
import com.ktgj.hotel.utils.GaoDeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.hotel.service.IDicCityBrandService;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-09
 */
@Service
public class DicCityBrandServiceImpl implements IDicCityBrandService 
{
    @Autowired
    private DicCityBrandMapper dicCityBrandMapper;

    @Autowired
    private DicHotPositionMapper dicHotPositionMapper;

    @Autowired
    private DicCircleMapper dicCircleMapper;

    @Autowired
    private DicAirportStationMapper dicAirportStationMapper;

    @Autowired
    private DicLankMarkMapper dicLankMarkMapper;

    @Autowired
    private DicSubwayStationMapper dicSubwayStationMapper;

    @Autowired
    private DicCityMapper dicCityMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public DicCityBrand selectDicCityBrandById(Long id)
    {
        return dicCityBrandMapper.selectDicCityBrandById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param dicCityBrand 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<DicCityBrand> selectDicCityBrandList(DicCityBrand dicCityBrand)
    {
        return dicCityBrandMapper.selectDicCityBrandList(dicCityBrand);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param dicCityBrand 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertDicCityBrand(DicCityBrand dicCityBrand)
    {
        dicCityBrand.setCreateTime(DateUtils.getNowDate());
        return dicCityBrandMapper.insertDicCityBrand(dicCityBrand);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param dicCityBrand 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateDicCityBrand(DicCityBrand dicCityBrand)
    {
        return dicCityBrandMapper.updateDicCityBrand(dicCityBrand);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteDicCityBrandByIds(Long[] ids)
    {
        return dicCityBrandMapper.deleteDicCityBrandByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteDicCityBrandById(Long id)
    {
        return dicCityBrandMapper.deleteDicCityBrandById(id);
    }

    /**
     * 站点
     * */
    @Override
    public List<BrandVo> queryCityIdByBrand(String cityName, String lng, String lat) throws Exception {
        Long cityId = 0l;
        if (cityName != null && !cityName.equals("")){
            cityId = dicCityMapper.cityId(cityName);
        }
        else if (lng != null && !lng.equals("") && lat != null && !lat.equals("")){
            cityId = cityId(lng, lat);
        }
        else {
            throw new Exception("缺少必要参数");
        }
        int[] types = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        List<BrandVo> brandVoList = new ArrayList<>();
        for (int type : types) {
            BrandVo brandVo;
            try {
                brandVo = type(type, cityId);
                brandVoList.add(brandVo);
            } catch (Exception e){
                throw new Exception(e.getMessage());
            }
        }
        return brandVoList;
    }

    public Long cityId(String lng, String lat) throws Exception {
        GaoDeUtil gaoDeUtil = new GaoDeUtil();
        String cityName = gaoDeUtil.cityName(lng, lat);
        Long cityId = dicCityMapper.cityId(cityName);
        if (cityId > 0){
            return cityId;
        }
        else {
            throw new Exception("获取城市id失败");
        }
    }

    public BrandVo type(int type, Long cityId) throws Exception {
        BrandVo brandVo = new BrandVo();
        List<CityBrandVo> cityBrandVoList = new ArrayList<>();
        switch (type){
            case 1://热门搜索
                brandVo.setType(HotelEnum.RegionalLocation.PopularSearches.getValue());
                List<DicHotPosition> dicHotPositionList = dicHotPositionMapper.popularAreaList(cityId);
                if (dicHotPositionList != null && dicHotPositionList.size() > 0){
                    for (DicHotPosition dicHotPosition : dicHotPositionList) {
                        CityBrandVo cityBrandVo = new CityBrandVo();
                        cityBrandVo.setId(dicHotPosition.getId());
                        cityBrandVo.setBrandNames(dicHotPosition.getPositionName());
                        cityBrandVoList.add(cityBrandVo);
                    }
                    break;
                }
            case 2://热门酒店
                brandVo.setType(HotelEnum.RegionalLocation.PopularHotels.getValue());
                break;
            case 3://商圈
                brandVo.setType(HotelEnum.RegionalLocation.BusinessDistrict.getValue());
                List<DicCircle> dicCircles = dicCircleMapper.dicCircleList(cityId);
                if (dicCircles != null && dicCircles.size() > 0){
                    for (DicCircle dicCircle : dicCircles) {
                        CityBrandVo cityBrandVo = new CityBrandVo();
                        cityBrandVo.setId(dicCircle.getId());
                        cityBrandVo.setBrandNames(dicCircle.getCircleName());
                        cityBrandVoList.add(cityBrandVo);
                    }
                    break;
                }
            case 4://机场车站
                brandVo.setType(HotelEnum.RegionalLocation.AirportStation.getValue());
                List<DicAirportStation> dicAirportStations = dicAirportStationMapper.selectDicAirportStation(cityId);
                if (dicAirportStations != null &&dicAirportStations.size() > 0){
                    for (DicAirportStation dicAirportStation : dicAirportStations) {
                        CityBrandVo cityBrandVo = new CityBrandVo();
                        cityBrandVo.setId(dicAirportStation.getId());
                        cityBrandVo.setBrandNames(dicAirportStation.getAirportStationName());
                        cityBrandVoList.add(cityBrandVo);
                    }
                    break;
                }
            case 5://医院
            case 6://景点
            case 7://大学
                brandVo.setType(HotelEnum.RegionalLocation.Hospital.getValue());
                List<DicLankMark> dicLankMarkList = dicLankMarkMapper.hospital(cityId);
                if (dicLankMarkList != null &&dicLankMarkList.size() > 0){
                    for (DicLankMark dicLankMark : dicLankMarkList) {
                        CityBrandVo cityBrandVo = new CityBrandVo();
                        cityBrandVo.setId(dicLankMark.getId());
                        cityBrandVo.setBrandNames(dicLankMark.getLandMarkName());
                        cityBrandVoList.add(cityBrandVo);
                    }
                    break;
                }
            case 8://地铁站
                brandVo.setType(HotelEnum.RegionalLocation.SubwayStation.getValue());
                List<DicSubwayStation> dicSubwayStations = dicSubwayStationMapper.dicSubwayStationList(cityId);
                if (dicSubwayStations != null && dicSubwayStations.size() > 0){
                    for (DicSubwayStation dicSubwayStation : dicSubwayStations) {
                        CityBrandVo cityBrandVo = new CityBrandVo();
                        cityBrandVo.setId(dicSubwayStation.getId());
                        cityBrandVo.setBrandNames(dicSubwayStation.getStationName());
                        cityBrandVoList.add(cityBrandVo);
                    }
                    break;
                }
            case 9://酒店品牌
                brandVo.setType(HotelEnum.RegionalLocation.HotelBrand.getValue());
                List<DicCityBrand> dicCityBrands = dicCityBrandMapper.cityIdByBrand(cityId);
                if (dicCityBrands != null && dicCityBrands.size() > 0){
                    for (DicCityBrand dicCityBrand : dicCityBrands) {
                        CityBrandVo cityBrandVo = new CityBrandVo();
                        cityBrandVo.setId(dicCityBrand.getId());
                        cityBrandVo.setBrandNames(dicCityBrand.getBrandName());
                        cityBrandVoList.add(cityBrandVo);
                    }
                    break;
                }
        }

        brandVo.setCityBrandVoList(cityBrandVoList);
        return brandVo;
    }
}
